//
//  CollectionView.m
//  test
//
//  Created by huichen on 2018/6/4.
//  Copyright © 2018年 huichen. All rights reserved.
//

#import "CellModel.h"
#import "CollectionView.h"
#import "CollectionViewCell.h"
#import "CollectionViewLayout.h"

@interface CollectionView ()<
UICollectionViewDelegate,
UICollectionViewDataSource,
CollectionViewLayoutDelegate
>

/**
 *  行数
 */
@property (nonatomic, assign) NSInteger rowNumber;

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) CollectionViewLayout *collectionlayout;

@property (nonatomic,strong) NSMutableArray<CellModel *> *dataArray;

@property (nonatomic,strong) UILongPressGestureRecognizer *longPressGest;


@end

    
@implementation CollectionView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
       [self configureUIAppearance];

    }
    return self;
}

- (void)configureUIAppearance{

    // 默认行数、间距
    self.rowNumber = 2;
    self.rowSpacing = 10.0f;
    self.lineSpacing = 10.0f;
    self.sectionInset = UIEdgeInsetsMake(10,10,10,10);
    self.backgroundColor = [UIColor whiteColor];
    _dataArray = [NSMutableArray array];
}

- (instancetype)initWithFrame:(CGRect)frame rowNumber:(NSInteger)rowNumber
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // 默认行数、间距
        self.rowNumber = rowNumber;
        self.rowSpacing = 10.0f;
        self.lineSpacing = 10.0f;
        self.sectionInset = UIEdgeInsetsMake(10,10,10,10);
        self.backgroundColor = [UIColor whiteColor];
        _dataArray = [NSMutableArray array];
    }
    return self;
}


- (instancetype)initWithPointX:(CGFloat)pX pointY:(CGFloat)pY width:(CGFloat)width rowNum:(NSInteger)rowNum rowHeight:(CGFloat)rowHeight{
    self = [super init];
    if (self) {
      
        // 默认行数、间距
        self.rowNumber = rowNum;
        self.rowSpacing = 10.0f;
        self.lineSpacing = 10.0f;
        self.sectionInset = UIEdgeInsetsMake(10,10,10,10);
        self.backgroundColor = [UIColor whiteColor];
        self.frame = CGRectMake(pX, pY, width, self.sectionInset.top + self.sectionInset.bottom + self.rowSpacing * (self.rowNumber - 1) + rowHeight *self.rowNumber);
        _dataArray = [NSMutableArray array];
    }
    return self;
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
    
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"connectionViewCell" forIndexPath:indexPath];
    cell.dataModel = self.dataArray[indexPath .row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"选中了第%ld个item",indexPath.row);
}

- (void)longPressGesture:(UILongPressGestureRecognizer *)longPreGest{

     NSIndexPath *selectIndexPath = [self.collectionView indexPathForItemAtPoint:[self.longPressGest locationInView:self.collectionView]];
    // 找到当前的cell
    CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:selectIndexPath];

    // 定义cell的时候btn是隐藏的, 在这里设置为NO
    [cell.deleteBtn setHidden:NO];

    cell.deleteBtn.tag = selectIndexPath.item;
    //添加删除的点击事件
    [cell.deleteBtn addTarget:self action:@selector(btnDelete:) forControlEvents:UIControlEventTouchUpInside];
}
   
- (void)btnDelete:(UIButton *)btn{

   //cell的隐藏删除设置
    NSIndexPath *selectIndexPath = [self.collectionView indexPathForItemAtPoint:[self.longPressGest locationInView:self.collectionView]];
    // 找到当前的cell
    CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:selectIndexPath];
    cell.deleteBtn.hidden = YES;
    
    //取出源item数据
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.dataArray];
    id objc = [tempArray objectAtIndex:btn.tag];
    //从资源数组中移除该数据
    [tempArray removeObject:objc];
    [self updateData:tempArray];
}

#pragma mark - CollectionViewLayoutDelegate
- (void)collectionViewLayoutChangeSuperViewHeight:(CGFloat)height{

    CGRect frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, height);
    self.frame = frame;
    self.collectionView.frame = CGRectMake(0, 0, self.frame.size.width ,self.frame.size.height);
}

 - (UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        self.collectionlayout = [[CollectionViewLayout alloc] initWithCollectionViewHeight:self.frame.size.height];
        self.collectionlayout.rowNumber = self.rowNumber; //行数
        self.collectionlayout.rowSpacing = self.rowSpacing; //行间距
        self.collectionlayout.lineSpacing = self.lineSpacing; //列间距
        self.collectionlayout.sectionInset = self.sectionInset;// 内边距
        self.collectionlayout.delegate = self;
    
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width ,self.frame.size.height) collectionViewLayout:self.collectionlayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"connectionViewCell"];
        __weak typeof (self) wSelf = self;
        [self.collectionlayout computeIndexCellHeightWithWidthBlock:^CGFloat(NSIndexPath *indexPath, CGFloat height) {
            if (!wSelf) return 0;
            CellModel *model = wSelf.dataArray[indexPath.row];
            return model.titleWidth;
        }];
       self.longPressGest = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
       [self.collectionView addGestureRecognizer:self.longPressGest];
    }
    return _collectionView;
}


- (void)updateData:(NSMutableArray<CellModel *> *)dataArray{

    _dataArray = dataArray;
    [self addSubview:self.collectionView];
    [self.collectionView reloadData];
    self.collectionlayout.currentowNumber = (self.dataArray.count < self.rowNumber) ? self.dataArray.count : self.rowNumber;
    self.hidden = (_dataArray.count == 0) ? YES : NO ;
}


@end
